package com.yange.accountbook.popup

import android.content.Context
import android.text.TextUtils
import android.view.View
import android.view.ViewGroup
import android.widget.*
import com.lxj.xpopup.XPopup
import com.lxj.xpopup.core.BottomPopupView
import com.yange.accountbook.R
import com.yange.accountbook.entity.ModuleEntity
import com.yange.accountbook.net.Api
import com.yange.accountbook.net.BaseResponse
import com.yange.accountbook.net.api.RetrofitClient
import com.yange.accountbook.net.observable.RecObserver
import com.yange.accountbook.utils.showToast

class AddMoneyPop(context: Context) : BottomPopupView(context) {


    lateinit var editMoney: EditText
    lateinit var editRemark: EditText
    lateinit var splitGroup: ViewGroup
    lateinit var splitModule: TextView
    lateinit var btnCommit: Button
    lateinit var rb1: RadioButton
    lateinit var rb2: RadioButton
    lateinit var rb3: RadioButton
    lateinit var radioGroup: RadioGroup

    lateinit var dismissListener: DismissListener
    var bookId = 0
    var splitId = 0
    var accountId = 0
    var type = "OUT"

    interface DismissListener {
        fun dismiss()
    }

    constructor(context: Context, bookId: Int, accountId: Int, addListener: DismissListener) : this(
        context
    ) {
        this.dismissListener = addListener
        this.bookId = bookId
        this.accountId = accountId
    }

    override fun getImplLayoutId() = R.layout.pop_add_money_layout

    override fun onCreate() {
        super.onCreate()
        editMoney = findViewById(R.id.edit_book_money)
        editRemark = findViewById(R.id.edit_remark)
        btnCommit = findViewById(R.id.btn_commit)
        splitModule = findViewById(R.id.tv_splitModule)
        splitGroup = findViewById(R.id.ll_split)
        rb1 = findViewById(R.id.rb1)
        rb2 = findViewById(R.id.rb2)
        rb3 = findViewById(R.id.rb3)
        radioGroup = findViewById(R.id.radio_group)
        radioGroup.setOnCheckedChangeListener { radioGroup, i ->
            when (i) {
                R.id.rb1 -> {
                    type = "OUT"
                    splitGroup.setVisibility(View.GONE)
                }
                R.id.rb2 -> {
                    type = "IN"
                    splitGroup.setVisibility(View.GONE)
                }
                R.id.rb3 -> {
                    type = "SPLIT"
                    splitGroup.setVisibility(View.VISIBLE)
                    getSpitModule()
                }
            }
        }
        btnCommit.setOnClickListener {
            if (TextUtils.isEmpty(editMoney.text)) {
                showToast("请输入金额")
                return@setOnClickListener
            }
            if (TextUtils.isEmpty(editRemark.text)) {
                showToast("请输入备注")
                return@setOnClickListener
            }
            if (type.equals("SPLIT") && splitId == 0) {
                showToast("请选择分账模式")
                return@setOnClickListener
            }
            commitData()

        }
        splitModule.setOnClickListener {
            getSpitModule()
        }
    }

    override fun onDismiss() {
        super.onDismiss()
        dismissListener?.dismiss()
    }


    private fun commitData() {
        if (type.equals("SPLIT")) {
            RetrofitClient.getInstance().invokePostBody(
                context, Api.ACCOUNT_RECORD_SPLIT, mapOf(
                    "accountId" to accountId,
                    "money" to editMoney.text.toString().toDouble(),
                    "name" to editRemark.text.toString(),
                    "splitId" to splitId,
                    "type" to "IN"
                )
            ).subscribe(object : RecObserver<BaseResponse<Any>>(context, true, false) {
                override fun onSuccess(t: BaseResponse<Any>?) {
                    dismiss()
                }

                override fun onFail(msg: String, code: Int) {
                    showToast(msg)
                }
            })
        } else {
            RetrofitClient.getInstance().invokePostBody(
                context, Api.ACCOUNT_RECORD_SAVE, mapOf(
                    "accountId" to accountId,
                    "money" to editMoney.text.toString().toDouble(),
                    "name" to editRemark.text.toString(),
                    "type" to type
                )
            ).subscribe(object : RecObserver<BaseResponse<Any>>(context, true, false) {
                override fun onSuccess(t: BaseResponse<Any>?) {
                    dismiss()
                }

                override fun onFail(msg: String, code: Int) {
                    showToast(msg)
                }
            })
        }


    }

    /**
     * 获取账本详情
     */
    private fun getSpitModule() {
        RetrofitClient.getInstance().invokePostBody(context, Api.SPLIT_ACCOUNT_LIST, mapOf())
            .subscribe(object :
                RecObserver<BaseResponse<List<ModuleEntity>>>(context, true, false) {
                override fun onSuccess(t: BaseResponse<List<ModuleEntity>>?) {
                    if (!t?.data.isNullOrEmpty()) {
                        XPopup.Builder(context)
                            .atView(splitModule)
                            .autoOpenSoftInput(true)
                            .dismissOnTouchOutside(true)
                            .asCustom(
                                ChoiceSplitModulePopup(context,
                                    t?.data as MutableList<ModuleEntity>,
                                    object : ChoiceSplitModulePopup.OnItemClickListener {
                                        override fun itemClick(item: ModuleEntity) {
                                            splitModule.text = item.name
                                            splitId = item.id
                                        }
                                    })
                            )
                            .show()
                    }
                }

                override fun onFail(msg: String, code: Int) {
                }
            })
    }

}